package com.aube.plugin.figure;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.aube.plugin.origin.DataConvertUtil;
import com.aube.plugin.origin.R;
import com.huyn.baseframework.dynamicload.DLBridge;
import com.huyn.baseframework.dynamicload.DLLayout;
import com.huyn.baseframework.dynamicload.IPositionManager;
import com.huyn.baseframework.statesync.ActorStateAsyncHelper;
import com.huyn.baseframework.statesync.IDataObserver;
import com.huyn.baseframework.utils.StringUtils;
import com.huyn.baseframework.utils.SysUtil;
import com.huyn.baseframework.utils.UserUtil;

/**
 * Created by huyaonan on 16/5/25.
 */
public class ParallaxTopBarLayout extends DLLayout implements IDataObserver {

    private ImageView mBack, mFavorImg;
    private TextView mTitle, mFavorCount;
    private View mFavor;

    private int space = 40;
    private int mTitleOffsetX = 0;
    private int mTitleOffsetY = 0;
    private int MAX_OFFSET_X = 0;
    private int mTopbarHeight = 0;

    private boolean isFavored = false;
    private int statusBarHeight = 0;

    public ParallaxTopBarLayout(Context context) {
        this(context, null);
    }
    public ParallaxTopBarLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public ParallaxTopBarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initViews();
    }

    @Override
    public void initViews() {
        space = getResources().getDimensionPixelOffset(R.dimen.widget_margin);
        mTopbarHeight = getResources().getDimensionPixelOffset(R.dimen.actionbar_height);
        mTitleOffsetX = getResources().getDimensionPixelOffset(R.dimen.widget_margin);

        statusBarHeight = SysUtil.getStatusHeight(context);

        setupData();
    }

    @Override
    public void fillData(Object o) {
    }

    private void setupData() {
        mBack = new ImageView(context);
        mBack.setScaleType(ImageView.ScaleType.CENTER);
        setImgWithRes(mBack, R.drawable.icon_new_back);
        addView(mBack);

        mTitle = new TextView(context);
        mTitle.setTextSize(20);
        mTitle.setTextColor(Color.WHITE);
        mTitle.setSingleLine(true);
        mTitle.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        addView(mTitle);

        mFavor = inflate(R.layout.item_favor, null, false);
        mFavorCount = (TextView) mFavor.findViewById(R.id.favor_count);
        mFavorImg = (ImageView) mFavor.findViewById(R.id.favor_icon);
        setImgWithRes(mFavorImg, R.drawable.icon_thumbfw);
        addView(mFavor);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if(mBack == null)
            return;

        mBack.measure(0, 0);
        mFavor.measure(0, MeasureSpec.makeMeasureSpec(mTopbarHeight, MeasureSpec.EXACTLY));
        int imgWidth = mBack.getMeasuredWidth();
        int favorWidth = mFavor.getMeasuredWidth();
        int width = getMeasuredWidth();
        MAX_OFFSET_X = imgWidth + 2*space;
        int titleWidth = width - MAX_OFFSET_X - space*2 - favorWidth;
        mTitle.measure(MeasureSpec.makeMeasureSpec(titleWidth, MeasureSpec.EXACTLY), 0);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if(mBack == null)
            return;

        int backY = statusBarHeight + (mTopbarHeight-mBack.getMeasuredHeight())/2;
        mBack.layout(l+space, t+backY, l+space+mBack.getMeasuredWidth(), t+mBack.getMeasuredHeight()+backY);

        mTitleOffsetY = getHeight()-mTopbarHeight+(mTopbarHeight-mTitle.getHeight())/2;
        mTitle.layout(l+mTitleOffsetX, t+mTitleOffsetY-offsety, l+mTitleOffsetX+mTitle.getMeasuredWidth(), t+mTitleOffsetY+mTitle.getMeasuredHeight()-offsety);

        int offsetFavorY = getHeight()-mTopbarHeight+(mTopbarHeight-mFavor.getHeight())/2;
        mFavor.layout(r - mFavor.getMeasuredWidth(), t + offsetFavorY, r, t + offsetFavorY + mFavor.getMeasuredHeight());
    }

    private int offsety=0;
    public void updateTitleOffset(int offset) {
        updateTitleTranslation(offset);
    }

    public void updateTitleTranslation(int offset) {
        if(offset >= getHeight()-2*mTopbarHeight -statusBarHeight) {
//            int offsetX = (int) (offset * 1f * (MAX_OFFSET_X - mTitleOffsetX) / (getHeight() - mTopbarHeight - statusBarHeight));
            int offsetX = (int) ((offset-getHeight()+2*mTopbarHeight+statusBarHeight) * 1f * (MAX_OFFSET_X - mTitleOffsetX) / (mTopbarHeight));
            mTitle.setTranslationX(offsetX);
        } else {
            mTitle.setTranslationX(0);
        }
        mTitle.setTranslationY(-offset);
        mFavor.setTranslationY(-offset);
    }

    public void setTopbarHeight(int height) {
        this.mTopbarHeight = height;

        requestLayout();
    }

    private ActorStateAsyncHelper mHelper;
    public void updateContent(final DataConvertUtil.SimpleCharacter character, final ActorStateAsyncHelper helper, final View mRoot, final DLBridge bridge) {
        if(helper != null) {
            mHelper = helper;
            mHelper.updateactor(character.relatedId, character.praiseCount, character.isPraised());
            mHelper.registObserver(this);
        }

        if(mBridge == null)
            mBridge = bridge;

        if(mBack == null)
            return;

        this.isFavored = character.isPraised();
        if(StringUtils.isNotBlank(character.actorName))
            this.mTitle.setText(character.actorName);
        setFavorCount(character.praiseCount);
        this.mFavorImg.setImageResource(isFavored ? R.drawable.icon_thumbfb : R.drawable.icon_thumbfw);
        /**deprecate it for now*/
        this.mFavor.setVisibility(View.GONE);
//        this.mFavor.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if(mBridge == null)
//                    return;
//
//                if(mBridge.isUserLogin()) {
//                    if (mHelper != null) {
//                        mHelper.operate(character.relatedId);
//                        setFavorCount(mHelper.getFavorCount(character.relatedId, character.praiseCount, !isFavored));
//                    }
//                    voteAnimation(mFavorImg, new Animation.AnimationListener() {
//                        @Override
//                        public void onAnimationStart(Animation animation) {
//                            mFavorImg.setImageResource(isFavored ? R.drawable.icon_thumbfw : R.drawable.icon_thumbfb);
//                        }
//
//                        @Override
//                        public void onAnimationEnd(Animation animation) {
//                            isFavored = !isFavored;
//                        }
//
//                        @Override
//                        public void onAnimationRepeat(Animation animation) {
//
//                        }
//                    });
//                }
//            }
//        });

        this.mBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) context).finish();
            }
        });
    }

    public static void voteAnimation(final ImageView view, final Animation.AnimationListener listener) {
        Animation anim = AnimationUtils.loadAnimation(view.getContext(), R.anim.breath_anim);
        anim.setDuration(150);
        if (listener != null)
            anim.setAnimationListener(listener);
        view.startAnimation(anim);
    }

    private void setFavorCount(int count) {
        if(count > 0)
            mFavorCount.setText(count + "");
        else
            mFavorCount.setText("");
    }

    @Override
    public void stateUpdate() {

    }

    public void onDestroy() {
        if(mHelper != null)
            mHelper.unregistObserver(this);
    }

    @Override
    public void notifyAnim(int i) {

    }

    @Override
    public void registToManager(IPositionManager iPositionManager) {

    }

    @Override
    public void unreistFromManager(IPositionManager iPositionManager) {

    }

    @Override
    public int[] getLocations() {
        return new int[0];
    }
}
